window.onload = function () {
    var inputData = []
    init(null, 1);
    function init(data, type){
        $.ajax({
            type: "POST",
            url: "/analyze/loanGrantingBasicSituationData",
            data:data,
            success: function (msg){
                if(msg.status=="false") {
                    return
                } else {
                    var result = msg.result
                    var result1 = msg.result1
                    if (type == 1) 
                    {
                        doCharts(result, type);
                    }
                    if (type == 2) 
                    {
                        doCharts(result1, type);
                    }
                }
            }
        });
    }

    function doCharts(allData, type) {
        var currentYear = new Date().getFullYear()
        var timelist = [currentYear-9,currentYear-8,currentYear-7,currentYear-6, currentYear-5, currentYear-4,currentYear-3,currentYear-2,currentYear-1, currentYear]
        var option4 = {
            baseOption: {
                timeline: {
                    axisType: 'category',
                    autoPlay: false,
                    currentIndex: (timelist.length-1),
                    playInterval: 1000,
                    data: timelist,
                    label: {
                        normal: {
                            color: '#ADADAD',
                        },
                    },
                },
                tooltip: {
                },
                legend: {
                    orient: 'vertical',
                    x: 'left',
                    data: ["一手现房", "一手期房", "二手现房"],
                    textStyle: {
                        color : "#ADADAD"
                    },
                },
                calculable : true,
                grid: {
                    top: 80,
                    bottom: 100,
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow',
                            label: {
                                show: true,
                                formatter: function (params) {
                                    return params.value.replace('\n', '');
                                }
                            }
                        }
                    }
                },
                series: [
                    {
                        name: '贷款发放数量',
                        type: 'pie',
                        center: ['50%', '40%'],
                        radius: '60%',
                        z: 100,
                        tooltip : {
                            formatter: "{a} <br/>{b} : {c} ({d}%)"
                        }
                    }
                ]
            },
            options: [
            ]
        };

        for (var i = 0; i < timelist.length; i++) {

            if (type == 1) 
            {
                option4.options[i] = {
                    series: [
                        {
                            data: [
                                {name: '一手现房', value: 0},
                                {name: '一手期房', value: 0},
                                {name: '二手现房', value: 0}
                            ]
                        }
                    ]
                }

                for (var j = 0; j < allData.length; j++) {
                    if (timelist[i] == allData[j].startDate) {
                        option4.options[i] = {
                            series: [
                                {
                                    data: [
                                        {name: '一手现房', value: allData[j].FIRST},
                                        {name: '一手期房', value: allData[j].FIRSTFORWARD},
                                        {name: '二手现房', value: allData[j].SECOND}
                                    ]
                                }
                            ]
                        }
                    }
                }
            }

            if (type == 2) 
            {
                option4.options[i] = {
                    series: [
                        {
                            data: [
                                {name: '首套', value: 0},
                                {name: '非首套', value: 0}
                            ]
                        }
                    ]
                }

                for (var j = 0; j < allData.length; j++) {
                    if (timelist[i] == allData[j].startDate) {
                        option4.options[i] = {
                            series: [
                                {
                                    data: [
                                        {name: '首套', value: allData[j].ISFIRSTHOUSE},
                                        {name: '非首套', value: allData[j].ISNOTFIRSTHOUSE}
                                    ]
                                }
                            ]
                        }
                    }
                }
            }
        }

        if (type == 1) 
        {
            option4.baseOption.legend.data = ["一手现房", "一手期房", "二手现房"]
        }

        if (type == 2) 
        {
            option4.baseOption.legend.data = ["首套", "非首套"]
        }

        var myChart = echarts.init($('#myChart4').get(0));
        myChart.setOption(option4);
        window.onresize = function () {
            myChart.resize()
        };
    }

    $('#searchData').click(function(){
        var citys = $('#city').val(),
            districts = $('#district').val(),
            loansBalanceStarts = $('#loansBalanceStart').val(),
            loansBalanceEnds =  $('#loansBalanceEnd').val(),
            loansAmountStarts = $('#loansAmountStart').val(),
            loansAmountEnds = $('#loansAmountEnd').val(),
            startDateStarts = $('#startDateStart').val(),
            startDateEnds = $('#startDateEnd').val(),
            loansDurations = $('#loansDuration').val(),
            remainTimes = $('#remainTime').val(),
            risk = $('#risk').val(),
            buildAreaEnds = $('#buildAreaEnd').val(),
            buildAreaStarts = $('#buildAreaStart').val(),
            endDateStarts = $('#endDateStart').val(),
            endDateEnds = $('#endDateEnd').val(),
            yearLength = 2,
            yearQuarter = "",
            inputData = {
                city:citys,
                district:districts,
                loansBalanceStart:loansBalanceStarts,
                loansBalanceEnd:loansBalanceEnds,
                loansAmountStart:loansAmountStarts,
                loansAmountEnd:loansAmountEnds,
                startDateStart:startDateStarts,
                startDateEnd:startDateEnds,
                loansDuration:loansDurations,
                remainTime:remainTimes,
                risk:risk,
                buildAreaEnd:buildAreaEnds,
                buildAreaStart:buildAreaStarts,
                endDateStart:endDateStarts,
                endDateEnd:endDateEnds,
                yearLength:yearLength,
                yearQuarter:yearQuarter
            };

        if($("#one").hasClass("active")) {
            init(inputData, 1)
        } else if($("#two").hasClass("active")){
            init(inputData, 2)
        }
    });

    $("#one").click(function(){
        init(inputData, 1);
    });
    $("#two").click(function(){
        init(inputData, 2);
    });
};